package cn.com.dhc.chesishanbigdemo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;

public class RegisterDao {

	// JDBC driver name and database URL
	static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
	static final String DB_URL = "jdbc:mysql://localhost/cssdemo";

	// Database credentials
	static final String USER = "root";
	static final String PASS = "root";

	/**
	 * Thread
	 */
	public RegisterDao() {
		try {
			Class.forName(JDBC_DRIVER);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public int insertUser(User user) {
		String username = user.getUsername();
		String password = user.getPswd();

		try {
			Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);

			// new 判断用户名是否已经存在
			String sql = "SELECT * FROM userinfo WHERE username=" + username;
			PreparedStatement prepareStatement1 = conn.prepareStatement(sql);// 每一句sql语句都对应一个Statement,否则会报越界错误
			ResultSet rs = prepareStatement1.executeQuery(sql);
			int count = 0;
			while (rs.next())
				count++;
			if (count == 0) {
				sql = "insert into userinfo(id,username, pswd) values (?,?,?)";
				PreparedStatement prepareStatement2 = conn
						.prepareStatement(sql);// 重新写了sql语句要重新写一个statement
				prepareStatement2.setString(1, UUID.randomUUID().toString()
						.substring(0, 11));
				prepareStatement2.setString(2, username);
				prepareStatement2.setString(3, password);
				int influenceCount = prepareStatement2.executeUpdate();
				return influenceCount;
			} else {
				System.out.println("用户名重复!");
			}
			return 0;

			// ending

			// String sql =
			// "insert into userinfo(id,username, pswd) values (?,?, ?)";
			// PreparedStatement prepareStatement = conn.prepareStatement(sql);
			/*
			 * sql = "insert into userinfo(id,username, pswd) values (?,?, ?)";
			 * prepareStatement.setString(1, UUID.randomUUID().toString()
			 * .substring(0, 11)); prepareStatement.setString(2, username);
			 * prepareStatement.setString(3, password);
			 * 
			 * int influenceCount = prepareStatement.executeUpdate(); return
			 * influenceCount;
			 */
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}

		// select
		// return User (ResultSet)
	}

	// 修改的 start
	public int insertInformation(Information information) {
		String sex = information.getSex();
		String age = information.getAge();
		String phone = information.getPhone();
		String mail = information.getMail();
		try {
			Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
			String sql = "insert into information(id, sex, age,phone,mail) values (?, ?,?,?,?)";
			PreparedStatement prepareStatement = conn.prepareStatement(sql);
			prepareStatement.setString(1, UUID.randomUUID().toString()
					.substring(0, 11));
			prepareStatement.setString(2, sex);
			prepareStatement.setString(3, age);
			prepareStatement.setString(4, phone);
			prepareStatement.setString(5, mail);
			int influenceCount = prepareStatement.executeUpdate();
			return influenceCount;
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	// ending

}
